<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p></p>
    <script>
        /**
         * 方法名：ajax
         * 参数1：路径
         * 参数2：数据
         * 参数3：回调函数
         * */

        // 封装函数
        function ajax(url,data,cb){
            let x = new XMLHttpRequest();
            //{c:"e",age:18} => c=e&age=18
            //Object.keys(data) =  [c,age]
            let str = Object.keys(data).map(item=>`${item}=${data[item]}`).join('&');
            x.open('GET',url+"?"+str);
            x.send();
            x.responseType = 'json';
            x.onreadystatechange = function(){
                if(x.readyState === 4 && x.status>=200 && x.status<300){
                    cb(x.response)
                }
            }
        }


        // 如何使用
        ajax('https://v1.hitokoto.cn',{c:"e",age:18},(data)=>{
            console.log(data);

            document.querySelector('p').innerHTML = data.hitokoto;

        });

          
        
    </script>
</body>

</html>